package dp;


public class ds5 {
    public static void main(String[] args) {
        System.out.println(longestPalindrome2("cbbd"));
    }
    public static String longestPalindrome(String s) {
        char[] ch = s.toCharArray();
        int n = s.length();
        boolean[][] dp = new boolean[n][n];
        int b = 0, t = 0;
        int re = 1;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if(ch[i] == ch[j]){
                    if(j - i <= 1)
                        dp[i][j] = true;
                    else
                        dp[i][j] = dp[i + 1][j - 1];
                }
                if(dp[i][j]  && j - i + 1 > re){
                    re = j - i + 1;
                    b = i;
                    t = j;
                }
            }
        }
        return s.substring(b, t + 1);
    }
    static int b,e;
    static int len;
    public static String longestPalindrome2(String s) {

        for (int i = 0; i < s.length(); i++) {
            isvalid(s, i, i);
            isvalid(s, i, i + 1);
        }
        return s.substring(b,e);
    }
    public static void isvalid(String s, int i, int j){
        while(i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)){
            i--;
            j++;
        }
        if(len < j - i - 1){
            len = j - i - 1;
            b = i + 1;
            e = j;
        }
    }

}
